/*
 * $Id: ResolveAction.java 75 2002-11-05 08:13:49Z bpruessmann $
 * 
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.sync;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.api.IPerforceServerManager;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.internal.ui.sync.ITeamNode;
import org.eclipse.team.internal.ui.sync.SyncSet;

/**
  * @version 	$Revision: 75 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ResolveAction extends CatchupReleaseAction
{

	/** Constructor for ResolveAction. */
	public ResolveAction(PerforceSyncCompareInput model, ISelectionProvider sp, String label, Shell shell)
	{
		super(model, sp, label, shell);
	}

	/**
	 * @see CatchupReleaseAction#isEnabled(ITeamNode)
	 */
	protected boolean isEnabled(ITeamNode node)
	{
		return new SyncSet(new StructuredSelection(node)).hasConflicts();
	}

	/**
	 * @see CatchupReleaseAction#removeNonApplicableNodes(SyncSet, int)
	 */
	protected void removeNonApplicableNodes(SyncSet set, int syncMode)
	{
		set.removeIncomingNodes();
		set.removeOutgoingNodes();		
	}

	/**
	 * @see CatchupReleaseAction#run(SyncSet, IProgressMonitor)
	 */
	protected SyncSet run(SyncSet syncSet, IProgressMonitor monitor)
	{
		ITeamNode[] changed = syncSet.getChangedNodes();
		if (changed.length == 0) {
			return syncSet;
		}
		
		try
		{
			monitor.beginTask(null, changed.length);
						
			IResource[] resources = getIResourcesFrom(changed);
			for (int i = 0; i < resources.length; i++)
			{
				String spec = resources[i].getLocation().toOSString();
				
				IPerforceServerManager manager = 
					PerforceCorePlugin.getServerManager();
				IPerforceServer server =
					manager.getPerforceServer(resources[i]);
				
				server.resolve(spec, false);
			}
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
		}
		finally
		{
			monitor.done();
		}
		
		return syncSet;
	}

}
